import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { BaseResponse, Todo } from '../models/async'

export const api = createApi({
    reducerPath: 'api',
    baseQuery: fetchBaseQuery({ baseUrl: '/api' }),
    tagTypes: ['todo-list'],
    endpoints: (builder) => ({
        todoList: builder.query<BaseResponse<Todo[]>, void>({
            query: () => ({
                url: '/todo-list',
                method: 'GET'
            }),
            providesTags: ['todo-list'],
        }),
        addTodo: builder.mutation<BaseResponse<Todo>, { value: string }>({
          query: (todo) => ({
            url: '/todo',
            method:'POST',
            body: JSON.stringify(todo)
          }),
          invalidatesTags: ['todo-list'],
        }),
    }),
})
